#include "StompedReality/Components/RenderInfoComponent.h"
#include "StompedReality/Core/ComponentConstuctionHelper.h"
#include "StompedReality/Core/Delegate.h"
#include "StompedReality/Core/Graphics/BasicRenderFunctions.h"
#include "StompedReality/Core/Graphics/RenderSystem.h"
#include "StompedReality/Core/IGame.h"
#include "StompedReality/Core/TypeSystem/TypeSystem.h"

CreateRegDummy( RenderInfoComponent )

StompedReality::components::RenderInfoComponent::RenderInfoComponent( ) :
  Component ( )
{
}

StompedReality::components::RenderInfoComponent::~RenderInfoComponent( )
{
}

void StompedReality::components::RenderInfoComponent::Draw( )
{
  this->m_Game->RenderSystem( )->QueueRenderGroup( &(this->m_RenderGroup) );
}

void StompedReality::components::RenderInfoComponent::OnCreation( )
{
  Component::OnCreation( );
  this->m_Layer = 0;
  this->m_Translation = StompedReality::Math::Vector2 ( 0, 0 );
  this->m_Depth = 0.0f;
  this->m_Rotation = 0.0f;
}

void StompedReality::components::RenderInfoComponent::OnDestruction( )
{
}

void StompedReality::components::RenderInfoComponent::InitializeComponent(
  StompedReality::Core::ComponentConstuctionHelper* constructionHelper )
{
  this->m_RenderGroup.SetRenderOrder( this->m_Game->RenderSystem( )->CalculateKey ( this->m_Layer, this->m_Depth ) );

  StompedReality::Graphics::Functions::TranslateRotate* pushFunction =
    new StompedReality::Graphics::Functions::TranslateRotate( 0, true );

  if ( constructionHelper->IsStaticRenderable( ) )
  {
    this->CopyRenderData ( pushFunction );
  }
  else
  {
    pushFunction->CopyDelegate = StompedReality::CreateDelegate ( this, &RenderInfoComponent::CopyRenderData );
  }

  this->AddRenderFunction( pushFunction );
  this->AddRenderFunction( new StompedReality::Graphics::Functions::PopMatrix( 200 ) );
}

void StompedReality::components::RenderInfoComponent::Update ( )
{
}

bool StompedReality::components::RenderInfoComponent::ProcessMessage ( StompedReality::Core::Message* msg )
{
  return true;
}

StompedReality::Math::Vector2 StompedReality::components::RenderInfoComponent::GetTranslation( )
{
  return this->m_Translation;
}

void StompedReality::components::RenderInfoComponent::SetTranslation ( StompedReality::Math::Vector2 translation )
{
  this->m_Translation = translation;
}

float StompedReality::components::RenderInfoComponent::GetRotation( )
{
  return this->m_Rotation;
}

void StompedReality::components::RenderInfoComponent::SetRotation ( float rotation )
{
  this->m_Rotation = rotation;
}

char StompedReality::components::RenderInfoComponent::GetLayer( )
{
  return this->m_Layer;
}

void StompedReality::components::RenderInfoComponent::SetLayer ( char layer )
{
  this->m_Layer = layer;
}

float StompedReality::components::RenderInfoComponent::GetDepth( )
{
  return this->m_Depth;
}

void StompedReality::components::RenderInfoComponent::SetDepth ( float depth )
{
  this->m_Depth = depth;
}

void StompedReality::components::RenderInfoComponent::AddRenderFunction(
  StompedReality::Graphics::RenderFunction* function ){
  this->m_RenderGroup.AddRenderFunction( function );
}

void StompedReality::components::RenderInfoComponent::SortRenderFunctions ( )
{
  this->m_RenderGroup.SortFunctions  ( );
}

void StompedReality::components::RenderInfoComponent::CopyRenderData (
  StompedReality::Graphics::Functions::TranslateRotate* fObject )
{
  fObject->Translation = this->m_Translation;
  fObject->Rotation = this->m_Rotation;
  fObject->Depth = this->m_Depth;
}

int StompedReality::components::RenderInfoComponentReg::registeType( )
{
  return ( int ) ( StompedReality::type_system::TypeSystem::CreateTypeInfo<RenderInfoComponent> (
                     "RenderInfoComponent" )->
                   addTypeField ( StompedReality::type_system::TypeField ( "layer",
                                                                           &RenderInfoComponent::m_Layer ) )->
                   addTypeField ( StompedReality::type_system::TypeField ( "translation",
                                                                           &RenderInfoComponent::m_Translation ) )->
                   addTypeField ( StompedReality::type_system::TypeField ( "depth",
                                                                           &RenderInfoComponent::m_Depth ) )->
                   addTypeField ( StompedReality::type_system::TypeField ( "rotation",
                                                                           &RenderInfoComponent::m_Rotation ) ) );
}

static int typeReg = StompedReality::components::RenderInfoComponentReg::registeType( );
